/*
 * Copyright (C) 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package pl.edu.agh.pros.node;

import java.util.HashSet;
import java.util.Set;

import org.ros.namespace.GraphName;
import org.ros.node.AbstractNodeMain;
import org.ros.node.ConnectedNode;
import org.ros.node.topic.Publisher;

public class Talker extends AbstractNodeMain {

	private Set<String> sentMessages = new HashSet<String>();
	private final String prefix;

	public Talker(String prefix) {
		this.prefix = prefix;
	}

	@Override
	public GraphName getDefaultNodeName() {
		return new GraphName("rosjava_tutorial_pubsub/talker"); //$NON-NLS-1$
	}

	@Override
	public void onStart(ConnectedNode node) {
		try {
			Publisher<std_msgs.String> publisher = node.newPublisher("chatter", "std_msgs/String"); //$NON-NLS-1$ //$NON-NLS-2$
			Thread.sleep(10000);
			for (int i = 0; i < 200; i++) {
				std_msgs.String str = publisher.newMessage();
				str.setData(this.prefix + ": " + i); //$NON-NLS-1$
				publisher.publish(str);
				sentMessages.add(str.getData());
				Thread.sleep(10);
			}
		} catch (Exception e) {
			if (node != null) {
				node.getLog().fatal(e);
			} else {
				e.printStackTrace();
			}
		}
	}

	public Set<String> getSentMessages() {
		return sentMessages;
	}

}
